<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>fs.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_fs_fs.js.html">fs.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Wrappers for the HTML5 File API. These wrappers closely mirror
<a name="line17"></a> * the underlying APIs, but use Closure-style events and Deferred return values.
<a name="line18"></a> * Their existence also makes it possible to mock the FileSystem API for testing
<a name="line19"></a> * in browsers that don&#39;t support it natively.
<a name="line20"></a> *
<a name="line21"></a> * When adding public functions to anything under this namespace, be sure to add
<a name="line22"></a> * its mock counterpart to goog.testing.fs.
<a name="line23"></a> *
<a name="line24"></a> */
<a name="line25"></a>
<a name="line26"></a>goog.provide(&#39;goog.fs&#39;);
<a name="line27"></a>
<a name="line28"></a>goog.require(&#39;goog.array&#39;);
<a name="line29"></a>goog.require(&#39;goog.async.Deferred&#39;);
<a name="line30"></a>goog.require(&#39;goog.events&#39;);
<a name="line31"></a>goog.require(&#39;goog.fs.Error&#39;);
<a name="line32"></a>goog.require(&#39;goog.fs.FileReader&#39;);
<a name="line33"></a>goog.require(&#39;goog.fs.FileSystem&#39;);
<a name="line34"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>/**
<a name="line38"></a> * Get a wrapped FileSystem object.
<a name="line39"></a> *
<a name="line40"></a> * @param {goog.fs.FileSystemType_} type The type of the filesystem to get.
<a name="line41"></a> * @param {number} size The size requested for the filesystem, in bytes.
<a name="line42"></a> * @return {!goog.async.Deferred} The deferred {@link goog.fs.FileSystem}. If an
<a name="line43"></a> *     error occurs, the errback is called with a {@link goog.fs.Error}.
<a name="line44"></a> * @private
<a name="line45"></a> */
<a name="line46"></a>goog.fs.get_ = function(type, size) {
<a name="line47"></a>  var requestFileSystem = goog.global.requestFileSystem ||
<a name="line48"></a>      goog.global.webkitRequestFileSystem;
<a name="line49"></a>
<a name="line50"></a>  if (!goog.isFunction(requestFileSystem)) {
<a name="line51"></a>    return goog.async.Deferred.fail(new Error(&#39;File API unsupported&#39;));
<a name="line52"></a>  }
<a name="line53"></a>
<a name="line54"></a>  var d = new goog.async.Deferred();
<a name="line55"></a>  requestFileSystem(type, size, function(fs) {
<a name="line56"></a>    d.callback(new goog.fs.FileSystem(fs));
<a name="line57"></a>  }, function(err) {
<a name="line58"></a>    d.errback(new goog.fs.Error(err.code, &#39;requesting filesystem&#39;));
<a name="line59"></a>  });
<a name="line60"></a>  return d;
<a name="line61"></a>};
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>/**
<a name="line65"></a> * The two types of filesystem.
<a name="line66"></a> *
<a name="line67"></a> * @enum {number}
<a name="line68"></a> * @private
<a name="line69"></a> */
<a name="line70"></a>goog.fs.FileSystemType_ = {
<a name="line71"></a>  /**
<a name="line72"></a>   * A temporary filesystem may be deleted by the user agent at its discretion.
<a name="line73"></a>   */
<a name="line74"></a>  TEMPORARY: 0,
<a name="line75"></a>  /**
<a name="line76"></a>   * A persistent filesystem will never be deleted without the user&#39;s or
<a name="line77"></a>   * application&#39;s authorization.
<a name="line78"></a>   */
<a name="line79"></a>  PERSISTENT: 1
<a name="line80"></a>};
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * Returns a temporary FileSystem object. A temporary filesystem may be deleted
<a name="line85"></a> * by the user agent at its discretion.
<a name="line86"></a> *
<a name="line87"></a> * @param {number} size The size requested for the filesystem, in bytes.
<a name="line88"></a> * @return {!goog.async.Deferred} The deferred {@link goog.fs.FileSystem}. If an
<a name="line89"></a> *     error occurs, the errback is called with a {@link goog.fs.Error}.
<a name="line90"></a> */
<a name="line91"></a>goog.fs.getTemporary = function(size) {
<a name="line92"></a>  return goog.fs.get_(goog.fs.FileSystemType_.TEMPORARY, size);
<a name="line93"></a>};
<a name="line94"></a>
<a name="line95"></a>
<a name="line96"></a>/**
<a name="line97"></a> * Returns a persistent FileSystem object. A persistent filesystem will never be
<a name="line98"></a> * deleted without the user&#39;s or application&#39;s authorization.
<a name="line99"></a> *
<a name="line100"></a> * @param {number} size The size requested for the filesystem, in bytes.
<a name="line101"></a> * @return {!goog.async.Deferred} The deferred {@link goog.fs.FileSystem}. If an
<a name="line102"></a> *     error occurs, the errback is called with a {@link goog.fs.Error}.
<a name="line103"></a> */
<a name="line104"></a>goog.fs.getPersistent = function(size) {
<a name="line105"></a>  return goog.fs.get_(goog.fs.FileSystemType_.PERSISTENT, size);
<a name="line106"></a>};
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>/**
<a name="line110"></a> * Creates a blob URL for a blob object.
<a name="line111"></a> *
<a name="line112"></a> * @param {!Blob} blob The object for which to create the URL.
<a name="line113"></a> * @return {string} The URL for the object.
<a name="line114"></a> */
<a name="line115"></a>goog.fs.createObjectUrl = function(blob) {
<a name="line116"></a>  return goog.fs.getUrlObject_().createObjectURL(blob);
<a name="line117"></a>};
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/**
<a name="line121"></a> * Revokes a URL created by {@link goog.fs.createObjectUrl}.
<a name="line122"></a> *
<a name="line123"></a> * @param {string} url The URL to revoke.
<a name="line124"></a> */
<a name="line125"></a>goog.fs.revokeObjectUrl = function(url) {
<a name="line126"></a>  goog.fs.getUrlObject_().revokeObjectURL(url);
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * @typedef {!{createObjectURL: (function(!Blob): string),
<a name="line132"></a> *             revokeObjectURL: function(string): void}}
<a name="line133"></a> */
<a name="line134"></a>goog.fs.UrlObject_;
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/**
<a name="line138"></a> * Get the object that has the createObjectURL and revokeObjectURL functions for
<a name="line139"></a> * this browser.
<a name="line140"></a> *
<a name="line141"></a> * @return {goog.fs.UrlObject_} The object for this browser.
<a name="line142"></a> * @private
<a name="line143"></a> */
<a name="line144"></a>goog.fs.getUrlObject_ = function() {
<a name="line145"></a>  // This is what the spec says to do
<a name="line146"></a>  // http://dev.w3.org/2006/webapi/FileAPI/#dfn-createObjectURL
<a name="line147"></a>  if (goog.isDef(goog.global.URL) &amp;&amp;
<a name="line148"></a>      goog.isDef(goog.global.URL.createObjectURL)) {
<a name="line149"></a>    return /** @type {goog.fs.UrlObject_} */ (goog.global.URL);
<a name="line150"></a>  // This is what Chrome does (as of 10.0.648.6 dev)
<a name="line151"></a>  } else if (goog.isDef(goog.global.webkitURL) &amp;&amp;
<a name="line152"></a>             goog.isDef(goog.global.webkitURL.createObjectURL)) {
<a name="line153"></a>    return /** @type {goog.fs.UrlObject_} */ (goog.global.webkitURL);
<a name="line154"></a>  // This is what the spec used to say to do
<a name="line155"></a>  } else if (goog.isDef(goog.global.createObjectURL)) {
<a name="line156"></a>    return /** @type {goog.fs.UrlObject_} */ (goog.global);
<a name="line157"></a>  } else {
<a name="line158"></a>    throw Error(&#39;This browser doesn\&#39;t seem to support blob URLs&#39;);
<a name="line159"></a>  }
<a name="line160"></a>};
<a name="line161"></a>
<a name="line162"></a>
<a name="line163"></a>/**
<a name="line164"></a> * Concatenates one or more values together and converts them to a Blob.
<a name="line165"></a> *
<a name="line166"></a> * @param {...(string|!Blob|!ArrayBuffer)} var_args The values that will make up
<a name="line167"></a> *     the resulting blob.
<a name="line168"></a> * @return {!Blob} The blob.
<a name="line169"></a> */
<a name="line170"></a>goog.fs.getBlob = function(var_args) {
<a name="line171"></a>  var BlobBuilder = goog.global.BlobBuilder || goog.global.WebKitBlobBuilder;
<a name="line172"></a>
<a name="line173"></a>  if (goog.isDef(BlobBuilder)) {
<a name="line174"></a>    var bb = new BlobBuilder();
<a name="line175"></a>    for (var i = 0; i &lt; arguments.length; i++) {
<a name="line176"></a>      bb.append(arguments[i]);
<a name="line177"></a>    }
<a name="line178"></a>    return bb.getBlob();
<a name="line179"></a>  } else {
<a name="line180"></a>    return new Blob(goog.array.toArray(arguments));
<a name="line181"></a>  }
<a name="line182"></a>};
<a name="line183"></a>
<a name="line184"></a>
<a name="line185"></a>/**
<a name="line186"></a> * Converts a Blob or a File into a string. This should only be used when the
<a name="line187"></a> * blob is known to be small.
<a name="line188"></a> *
<a name="line189"></a> * @param {!Blob} blob The blob to convert.
<a name="line190"></a> * @param {string=} opt_encoding The name of the encoding to use.
<a name="line191"></a> * @return {!goog.async.Deferred} The deferred string. If an error occurrs, the
<a name="line192"></a> *     errback is called with a {@link goog.fs.Error}.
<a name="line193"></a> * @deprecated Use {@link goog.fs.FileReader.readAsText} instead.
<a name="line194"></a> */
<a name="line195"></a>goog.fs.blobToString = function(blob, opt_encoding) {
<a name="line196"></a>  return goog.fs.FileReader.readAsText(blob, opt_encoding);
<a name="line197"></a>};
<a name="line198"></a>
<a name="line199"></a>
<a name="line200"></a>/**
<a name="line201"></a> * Slices the blob. The returned blob contains data from the start byte
<a name="line202"></a> * (inclusive) till the end byte (exclusive). Negative indices can be used
<a name="line203"></a> * to count bytes from the end of the blob (-1 == blob.size - 1). Indices
<a name="line204"></a> * are always clamped to blob range. If end is omitted, all the data till
<a name="line205"></a> * the end of the blob is taken.
<a name="line206"></a> *
<a name="line207"></a> * @param {!Blob} blob The blob to be sliced.
<a name="line208"></a> * @param {number} start Index of the starting byte.
<a name="line209"></a> * @param {number=} opt_end Index of the ending byte.
<a name="line210"></a> * @return {Blob} The blob slice or null if not supported.
<a name="line211"></a> */
<a name="line212"></a>goog.fs.sliceBlob = function(blob, start, opt_end) {
<a name="line213"></a>  if (!goog.isDef(opt_end)) {
<a name="line214"></a>    opt_end = blob.size;
<a name="line215"></a>  }
<a name="line216"></a>  if (blob.webkitSlice) {
<a name="line217"></a>    // Natively accepts negative indices, clamping to the blob range and
<a name="line218"></a>    // range end is optional. See http://trac.webkit.org/changeset/83873
<a name="line219"></a>    return blob.webkitSlice(start, opt_end);
<a name="line220"></a>  } else if (blob.mozSlice) {
<a name="line221"></a>    // Natively accepts negative indices, clamping to the blob range and
<a name="line222"></a>    // range end is optional. See https://developer.mozilla.org/en/DOM/Blob
<a name="line223"></a>    // and http://hg.mozilla.org/mozilla-central/rev/dae833f4d934
<a name="line224"></a>    return blob.mozSlice(start, opt_end);
<a name="line225"></a>  } else if (blob.slice) {
<a name="line226"></a>    // Old versions of Firefox and Chrome use the original specification.
<a name="line227"></a>    // Negative indices are not accepted, only range end is clamped and
<a name="line228"></a>    // range end specification is obligatory.
<a name="line229"></a>    // See http://www.w3.org/TR/2009/WD-FileAPI-20091117/
<a name="line230"></a>    if ((goog.userAgent.GECKO &amp;&amp; !goog.userAgent.isVersion(&#39;13.0&#39;)) ||
<a name="line231"></a>        (goog.userAgent.WEBKIT &amp;&amp; !goog.userAgent.isVersion(&#39;537.1&#39;))) {
<a name="line232"></a>      if (start &lt; 0) {
<a name="line233"></a>        start += blob.size;
<a name="line234"></a>      }
<a name="line235"></a>      if (start &lt; 0) {
<a name="line236"></a>        start = 0;
<a name="line237"></a>      }
<a name="line238"></a>      if (opt_end &lt; 0) {
<a name="line239"></a>        opt_end += blob.size;
<a name="line240"></a>      }
<a name="line241"></a>      if (opt_end &lt; start) {
<a name="line242"></a>        opt_end = start;
<a name="line243"></a>      }
<a name="line244"></a>      return blob.slice(start, opt_end - start);
<a name="line245"></a>    }
<a name="line246"></a>    // IE and the latest versions of Firefox and Chrome use the new
<a name="line247"></a>    // specification. Natively accepts negative indices, clamping to the blob
<a name="line248"></a>    // range and range end is optional.
<a name="line249"></a>    // See http://dev.w3.org/2006/webapi/FileAPI/
<a name="line250"></a>    return blob.slice(start, opt_end);
<a name="line251"></a>  }
<a name="line252"></a>  return null;
<a name="line253"></a>};
</pre>


</body>
</html>
